//
//  CarViolationManager.m
//  EasyToo
//
//  Created by linshengqin on 13-9-9.
//
//

#import "CarViolationManager.h"
#import "CarViolationSearchRequest.h"
#import "CarViolationSearchResponse.h"
#import "ServerCommunicationManager.h"
#import "CarViolationOrderRequest.h"
#import "CarViolationOrderResponse.h"
@implementation CarViolationManager

static CarViolationManager* manager = nil;


+ (CarViolationManager*)shareManager
{
    if (manager == nil) {
        manager = [[CarViolationManager alloc] init];
    }
    return manager;
}

- (void)searchViolation:(NSString*)vehicleNo
           VehicleColor:(NSString*)color
                 RackNo:(NSString*)rackNo
                EnginNo:(NSString*)enginNo
{
    CarViolationSearchRequest *request = [[CarViolationSearchRequest alloc] initWithVehicleNo: vehicleNo VehicleColor:color RackNo:rackNo EnginNo:enginNo Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[CarViolationSearchResponse class]])
    {
        [self handleCarViolationSearchSuccess:(CarViolationSearchResponse*)response];
    }
    if([response isKindOfClass:[CarViolationOrderResponse class]])
    {
        [self handleCarViolationOrderSuccess:(CarViolationOrderResponse*)response];
    }

    
}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[CarViolationSearchResponse class]])
    {
     //   [self.delegate onSearchAllGameFinish:NO Games:nil];
    }
    
}


- (void)handleCarViolationSearchSuccess:(CarViolationSearchResponse*)response
{
//    NSMutableArray *gameInfoArray = response.gameInfoArray;
    if( [response.violationInfoArray count] <= 0)
    {
        [self.delegate onSearchCarViolationFinish:NO
                                        Violation:nil
                                          PostFee:nil];
        return;
        
    }
    NSMutableArray* violationInfoArray = response.violationInfoArray;
    NSString *postFee = response.postFee;
    [self.delegate onSearchCarViolationFinish:YES
                                    Violation:violationInfoArray
                                      PostFee:postFee];

}

- (void)handleCarViolationSearchFail:(CarViolationSearchResponse*)response
{
    [self.delegate onSearchCarViolationFinish:NO
                                    Violation:nil
                                      PostFee:nil];
}

-(void)orderViolation:(NSString*)vehicleNo
         VehicleColor:(NSString*)color
               RackNo:(NSString*)rackNo
              EnginNo:(NSString*)enginNo
             PostMode:(NSString*)postMode
              PostFee:(NSString*)postFee
             PhoneNum:(NSString*)phoneNum
          OrderDetail:(NSString*)orderDetail
          TotalAmount:(NSString*)totalAmount
{
    CarViolationOrderRequest* request = [[CarViolationOrderRequest alloc]initOrderVehicleNo:vehicleNo VehicleColor:color RackNo:rackNo EnginNo:enginNo PostMode:postMode PostFee:postFee PhoneNum:phoneNum OrderDetail:orderDetail TotalAmount:totalAmount Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];

}


- (void)handleCarViolationOrderSuccess:(CarViolationOrderResponse*)response
{
    NSString *tn = response.tn;
    
    if (tn)
    {
        [self.delegate onOrderViolationFinish:YES Tn:tn];
    }
    else
    {
        [self.delegate onOrderViolationFinish:NO Tn:nil];
    }

}

@end
